package com.freak.neteasecloudmusic.modules.find.recommend.songlist.all.detail;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.freak.neteasecloudmusic.R;
import com.freak.neteasecloudmusic.base.BaseAbstractSimpleActivity;
import com.freak.neteasecloudmusic.base.IActivityStatusBar;

import com.freak.neteasecloudmusic.dagger.component.DaggerSongClassifyListComponent;
import com.freak.neteasecloudmusic.modules.find.recommend.entity.HotSongListEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.entity.SongListEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.detail.DetailActivity;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.fragment.SongListFragmentContract;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.fragment.SongListFragmentPresenter;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.fragment.adapter.SongListFragmentAdapter;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.fragment.entity.PlaylistsEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.fragment.entity.RecommendEntity;
import com.freak.neteasecloudmusic.view.custom.toolbar.SimpleToolbar;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

public class SongClassifyListActivity extends BaseAbstractSimpleActivity implements IActivityStatusBar, SongListFragmentContract.View {
    @Inject
    SongListFragmentPresenter songListFragmentPresenter;
    @BindView(R.id.simpleToolbarSongClassifyList)
    SimpleToolbar simpleToolbarSongClassifyList;
    @BindView(R.id.recyclerViewSongClassifyList)
    RecyclerView recyclerViewSongClassifyList;
    private SongListFragmentAdapter mSongListAdapter;
    private List<MultiItemEntity> mList;
    public int mOffset = 0;
    public final int mLimit = 30;
    private Bundle bundle;
    private String cat;

    @Override
    protected int getLayout() {
        return R.layout.activity_song_classify_list;
    }

    @Override
    protected void initEventAndData() {
        loadData();
    }

    @Override
    protected void onDestroyRelease() {
        songListFragmentPresenter.detachView();
    }

    @Override
    protected void initView() {
        DaggerSongClassifyListComponent.builder().view(this).build().inject(this);
        simpleToolbarSongClassifyList.setLeftIconClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        bundle = getIntent().getExtras();
        if (bundle == null) {
            mActivity.finish();
            return;
        }
        cat = bundle.getString("cat");
        simpleToolbarSongClassifyList.setLeftTitleName(cat);
        mList = new ArrayList<>();
        recyclerViewSongClassifyList.setLayoutManager(new GridLayoutManager(mActivity, 3));
        mSongListAdapter = new SongListFragmentAdapter(mList, mActivity);
        mSongListAdapter.bindToRecyclerView(recyclerViewSongClassifyList);
        mSongListAdapter.setEmptyView(R.layout.view_no_data_common, recyclerViewSongClassifyList);
        mSongListAdapter.disableLoadMoreIfNotFullPage();
        recyclerViewSongClassifyList.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.right = 10;
                outRect.left = 10;
            }
        });
        recyclerViewSongClassifyList.setAdapter(mSongListAdapter);
        mSongListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadData();
            }
        }, recyclerViewSongClassifyList);
        mSongListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (adapter.getItemViewType(position)) {
                    case SongListFragmentAdapter.SONG_LIST:
                        PlaylistsEntity playlistsEntity = (PlaylistsEntity) adapter.getData().get(position);
                        Bundle bundle = new Bundle();
                        bundle.putString("id", playlistsEntity.getId());
                        gotoActivity(DetailActivity.class, false, bundle);
                        break;
                }
            }
        });

    }

    private void loadData() {
        songListFragmentPresenter.loadSongListCategoryList("new", cat, mLimit, mOffset);
    }


    @Override
    public int getStatusBarColor() {
        return ContextCompat.getColor(this, R.color.color_fbfbfb);
    }

    @Override
    public int getDrawableStatusBar() {
        return 0;
    }

    @Override
    public void loadSongListCategoryListSuccess(SongListEntity songListEntity) {
        if (mOffset == 0) {
            mList.clear();
        }
        if (songListEntity != null) {
            if (songListEntity.isMore()) {
                mOffset += mLimit;
            }
            if (mOffset >= songListEntity.getTotal()) {
                mSongListAdapter.loadMoreEnd();
            } else {
                mSongListAdapter.loadMoreComplete();
            }
            mList.addAll(songListEntity.getPlaylists());
        }
        mSongListAdapter.notifyDataSetChanged();
    }

    @Override
    public void loadHotSongListCategoryListSuccess(HotSongListEntity hotSongListEntity) {

    }

    @Override
    public void getRecommendSongListSuccess(RecommendEntity recommendEntity) {

    }

    @Override
    public void showToast(String toast) {

    }

}
